package com.ecoolex.ying.space.core.manager;

import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.pojo.Sorting;
import com.ecoolex.framework.common.pojo.Sortings;
import com.ecoolex.framework.common.util.BizAssert;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.ying.space.core.dao.FeedbackDAO;
import com.ecoolex.ying.space.entity.Feedback;
import com.ecoolex.ying.space.entity.Feedback.Column;
import com.ecoolex.ying.space.enums.FeedbackStatus;
import com.ecoolex.ying.space.entity.FeedbackExample;
import com.ecoolex.ying.space.pojo.AddFeedbackDTO;
import com.ecoolex.ying.space.pojo.EditFeedbackDTO;
import com.ecoolex.ying.space.pojo.QueryFeedbackDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.util.Date;

@Service
public class FeedbackManager {
    @Autowired
    private FeedbackDAO dao;

    public Feedback getById(Integer id) {
        return dao.selectByPrimaryKeyWithLogicalDelete(id, Feedback.NOT_DELETED);
    }

    public Pagination<Feedback> query(QueryFeedbackDTO query, Sorting[] sorts, Page page) {
        FeedbackExample example = new FeedbackExample();
        FeedbackExample.Criteria criteria = example.createCriteria();

        if (Check.notNull(query.getStatus())) {
            criteria.andStatusEqualTo(query.getStatus());
        }
        if (Check.notNullOrTrimEmpty(query.getContent())) {
            criteria.andContentLike('%' + query.getContent() + '%');
        }
        if (Check.notNullOrTrimEmpty(query.getContactPerson())) {
            criteria.andContactPersonLike('%' + query.getContactPerson() + '%');
        }
        if (Check.notNullOrTrimEmpty(query.getContactPhone())) {
            criteria.andContactPhoneEqualTo(query.getContactPhone());
        }
        if (Check.notNull(query.getUserId())) {
            criteria.andUserIdEqualTo(query.getUserId());
        }

        criteria.andDeletedEqualTo(Feedback.NOT_DELETED);

        example.orderBy(Sortings.builder(sorts).verify(Feedback.Column::valueOf).toSql());
        example.setOffset(page.getFirstIndex());
        example.setRows(page.getPageSize());

        return Pagination.create(page, dao.countByExample(example), () -> dao.selectByExample(example));
    }

    public Feedback add(AddFeedbackDTO record, String createUser) {
    	BizAssert.isTrue(Check.notNullOrEmpty(record.getContent()),ResultCode.VERIFY.build(10), "反馈内容为空");
        record.setStatus(FeedbackStatus.WAITINGPROCESSED.real());
        record.setDeleted(Feedback.NOT_DELETED);
        record.setCreateUser(createUser);
        record.setCreateTime(Date.from(Instant.now()));
        record.setUpdateUser(createUser);
        record.setUpdateTime(Date.from(Instant.now()));
        
        dao.insert(record);
        return record;
    }

    public Feedback update(EditFeedbackDTO record, String updateUser) {
    	Feedback feedback = getById(record.getId());
    	BizAssert.isTrue(Check.notNull(feedback), ResultCode.VERIFY.build(10),"反馈不存在");
    	
    	feedback.setStatus(record.getStatus());
        feedback.setResultInstructions(record.getResultInstructions());
        feedback.setUpdateUser(updateUser);
        feedback.setUpdateTime(Date.from(Instant.now()));
        dao.updateByPrimaryKeySelective(feedback, Column.status,Column.resultInstructions,Column.updateUser,Column.updateTime);
        return feedback;
    }

}